package com.yishu.Filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        String  userId = null;
        userId = String.valueOf(httpServletRequest.getSession().getAttribute("userId"));
        if(userId.equals("null")) {
            httpServletRequest.setAttribute("result","您还未登录,请先登录！");
            httpServletRequest.getRequestDispatcher("/userLogin.jsp").forward(servletRequest,servletResponse);
        }else {
            filterChain.doFilter(httpServletRequest,httpServletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
